package grp.test.controller;

import grp.test.common.Const;
import grp.test.common.ResponseCode;
import grp.test.common.ServerResponse;
import grp.test.pojo.Book;
import grp.test.pojo.Favorite;
import grp.test.pojo.User;
import grp.test.service.IFavoriteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/favorite/")
public class IFavoriteController {
    @Autowired
    IFavoriteService favoriteService;
    @RequestMapping(value = "addFavoriteBook.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<String> addFavoriteBook(Integer bookId, HttpSession session){
        User user = (User)session.getAttribute(Const.CURRENT_USER);
        if(user == null)
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),
                    "你还没有登陆，请登陆后再添加收藏");
        return favoriteService.addFavoriteBook(user.getUserId(), bookId);
    }
    @RequestMapping(value = "lookThroughBookList.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<List<Book>> lookThroughBookList(int pageSize, int pageNum,
                                                          HttpSession session) {
        User user = (User)session.getAttribute(Const.CURRENT_USER);
        if (user == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),
                    "你当前没有登陆，无法浏览收藏信息");
        }
        ServerResponse<List<Book>> listServerResponse =
                favoriteService.getbookList(user.getUserId());
        if (listServerResponse.isSuccess() != true)
            return listServerResponse;
        List<Book> bookList = listServerResponse.getData();
        if (bookList.size()<pageSize*pageNum){
            return listServerResponse;
        }
        List<Book> limBookList = bookList.subList(0,pageSize*pageNum);
        return ServerResponse.createBySuccess("数据读取成功",limBookList);
    }
    @RequestMapping(value = "removeFavoriteBook.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<String> removeFavoriteBook(int bookId, HttpSession session){
        User user = (User)session.getAttribute(Const.CURRENT_USER);
        if (user == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),
                    "你可能掉线了，请重新登陆");
        }
        return favoriteService.removeFavoriteBook(user.getUserId(),bookId);
    }
    @RequestMapping(value = "selectSingleFavorite.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<Favorite> selectSingleFavorite(int userId, int bookId){
        return favoriteService.selectSingleFavorite(userId, bookId);
    }
}
